﻿package com.tvein.sanguo.swc.action.role
{
	//flash
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import fl.controls.TextInput;
	import flash.text.TextFormat;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class RoleCreate extends MovieClip
	{
		public static const NAME:String = "Role-Create";

		private var sourceServer:String;
		private var effectVol:int = 100;
		private var sex:Boolean = true;
		private var ImageID:int;
		
		private var maleImageArea:int = 100;
		private var femaleImageArea:int = 74;
		
		private var txtFirstName:TextInput;
		private var txtLastName:TextInput;
		private var txtStyleName:TextInput;

		public function RoleCreate()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(KeyboardEvent.KEY_UP, _keyDownHandler);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			var style:TextFormat = new TextFormat();
			style.size = 20;
			style.font = "LiSu";
			style.align = "center";
			
			txtFirstName = new TextInput();
			txtFirstName.x = 370;
			txtFirstName.y = 143;
			txtFirstName.width = 100;
			txtFirstName.height = 24;
			txtFirstName.maxChars = 2;
			txtFirstName.restrict = "\u4e00-\u9fa5";
			txtFirstName.setStyle("textFormat", style);
			this.addChild(txtFirstName);
			
			txtLastName = new TextInput();
			txtLastName.x = 370;
			txtLastName.y = 213;
			txtLastName.width = 100;
			txtLastName.height = 24;
			txtLastName.maxChars = 2;
			txtLastName.restrict = "\u4e00-\u9fa5";
			txtLastName.setStyle("textFormat", style);
			this.addChild(txtLastName);	
			
			txtStyleName = new TextInput();
			txtStyleName.x = 370;
			txtStyleName.y = 283;
			txtStyleName.width = 100;
			txtStyleName.height = 24;
			txtStyleName.maxChars = 2;
			txtStyleName.restrict = "\u4e00-\u9fa5";
			txtStyleName.setStyle("textFormat", style);
			this.addChild(txtStyleName);
			
			btnNew.visible = false;
			btnMale.visible = false;
			
			btnHead.addEventListener(MouseEvent.CLICK, onClick);
			btnName.addEventListener(MouseEvent.CLICK, onClick);
			btnNew.addEventListener(MouseEvent.CLICK, onClick);
			btnBack.addEventListener(MouseEvent.CLICK, onClick);
			btnMale.addEventListener(MouseEvent.CLICK, onClick);
			btnFemale.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(600, 450);
			
			_resizeUI();
		}

		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setSource":
					sourceServer = params.Address;
					FaceLoad();
					break;
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setData":
					setData(params);
					break;
			}
		}
		
		private function setData(params:Object):void
		{
			if (params.Name == "GetImageArea")
			{
				for (var i:int = 0; i < params.Process.length; i++)
				{
					switch (params.Process[i].Type)
					{
						case "MaleImageArea":
							maleImageArea = int(params.Process[i].Value);
							break;
						case "FemaleImageArea":
							femaleImageArea = int(params.Process[i].Value);
							break;
					}
				}
			}
			
			if (params.Name == "GetRandName")
			{
				txtFirstName.text = params.Process[0].FirstName;
				txtLastName.text = params.Process[0].LastName;
				txtStyleName.text = params.Process[0].StyleName;
				
				btnName.visible = true;
				_keyDownHandler();
			}
		}
		
		private function FaceLoad():void
		{			
			if (sex)
			{
				ImageID = int(Math.random() * maleImageArea) + 10001;
			}
			else
			{
				ImageID = int(Math.random() * femaleImageArea) + 20001;
			}
			
			var faceloader:Loader = new Loader();
			faceloader.load(new URLRequest(sourceServer + "/SanGuo/role/head/" + ImageID + ".png"));
			faceloader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onFaceLoad);
		}

		private function _onFaceLoad(evt:Event):void
		{
			evt.target.removeEventListener(Event.COMPLETE, _onFaceLoad);
			
			evt.target.content.width = 240;
			evt.target.content.height = 240;
			evt.target.content.x = 38;
			evt.target.content.y = 78;
			
			this.addChild(evt.target.content);
			
			btnHead.visible = true;
		}

		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(KeyboardEvent.KEY_UP, _keyDownHandler);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnHead.removeEventListener(MouseEvent.CLICK, onClick);
			btnName.removeEventListener(MouseEvent.CLICK, onClick);
			btnNew.removeEventListener(MouseEvent.CLICK, onClick);
			btnBack.removeEventListener(MouseEvent.CLICK, onClick);
			btnMale.removeEventListener(MouseEvent.CLICK, onClick);
			btnFemale.removeEventListener(MouseEvent.CLICK, onClick);
		}

		private function _keyDownHandler(evt:KeyboardEvent = null):void
		{
			txtFirstName.text = txtFirstName.text.replace("　", "").replace(" ", "");
			txtLastName.text = txtLastName.text.replace("　", "").replace(" ", "");
			txtStyleName.text = txtStyleName.text.replace("　", "").replace(" ", "");
									
			btnNew.visible = (txtFirstName.text != "") && (txtLastName.text != "") && (txtStyleName.text != "");
		}

		private function _resizeUI(evt:Event = null):void
		{			
			this.x = (stage.stageWidth - this.width) * .5;
			
			if ((stage.stageHeight - this.height) > 100)
			{
				this.y = (stage.stageHeight - this.height) * .5;
			}
			else
			{
				this.y = 50;
			}
		}

		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnHead":
					btnHead.visible = false;
					FaceLoad();
					break;
				case "btnName":
					btnName.visible = false;
					dispatchEvent(new SwcEvent(NAME, ["-gameCommand", ["GetGameData(Action,GetRandName," + int(sex) + ");"]]));
					break;
				case "btnNew":
					dispatchEvent(new SwcEvent(NAME, ["-roleCreate", [txtFirstName.text, txtLastName.text, txtStyleName.text, ImageID, int(sex)]]));
					break;
				case "btnBack":
					dispatchEvent(new SwcEvent(NAME, ["-roleList"]));
					break;
				case "btnMale":
					btnMale.visible = false;
					btnFemale.visible = true;
					sex = true;
					FaceLoad();
					break;
				case "btnFemale":
					btnMale.visible = true;
					btnFemale.visible = false;
					sex = false;
					FaceLoad();
					break;
			}
		}
	}
}